package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {


    @Reference
    OrderSettingService orderSettingService;


    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){

        try {
            List<String[]> stringsList = POIUtils.readExcel(excelFile);
            //把string数组转换为orderSetting对象
            List<OrderSetting> orderSettingList = new ArrayList<>();
            //循环
            for (String[] strs : stringsList) {
                //一个数组对应一个OrderSetting
                OrderSetting orderSetting = new OrderSetting();
                //把字符串日期转换为日期类型
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                Date date = sdf.parse(strs[0]);
                //预约日期
                orderSetting.setOrderDate(date);
                //可预约人数
                orderSetting.setNumber(Integer.parseInt(strs[1]));

                //添加到集合中
                orderSettingList.add(orderSetting);
            }
            //发出请求，把orderSettingList写入到数据库
            orderSettingService.addOrderSettings(orderSettingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }

    }

    /**
     *
     * [
     *      {id: 1, orderDate :"2019-11-01" , number:"" ,reservations ： 0},
     *      {id: 1, orderDate :"2019-11-01" , number:"" ,reservations ： 0},
     *      {id: 1, orderDate :"2019-11-01" , number:"" ,reservations ： 0}
     * ]
     * @param month
     * @return
     */
    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);
            //转换结构
            List<Map<String,Object>> mapList = new ArrayList<>();
            //循环查询到的数据
            for (OrderSetting orderSetting : orderSettingList) {
                //一个ordersetting 对应一个map、集合
                Map<String,Object> map = new HashMap<>();
                SimpleDateFormat sdf = new SimpleDateFormat("dd");
                String date = sdf.format(orderSetting.getOrderDate());

                map.put("date", date);
                map.put("number", orderSetting.getNumber() );
                map.put("reservations", orderSetting.getReservations());
                //添加到集合中
                mapList.add(map);
            }
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            orderSettingService.addOrderSettings(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
